package dbms.ypr.user;

import dbms.ypr.SQLConstant;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String userDbFile = SQLConstant.getPath() + "\\user.txt";

    private String username;

    private String password;

    private List<TablePermissions> tablePermissions;

    private List<DataBasePermissions> dataBasePermissions;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<TablePermissions> getTablePermissions() {
        return tablePermissions;
    }

    public void setTablePermissions(List<TablePermissions> tablePermissions) {
        this.tablePermissions = tablePermissions;
    }

    public List<DataBasePermissions> getDataBasePermissions() {
        return dataBasePermissions;
    }

    public void setDataBasePermissions(List<DataBasePermissions> dataBasePermissions) {
        this.dataBasePermissions = dataBasePermissions;
    }

    public static User getLoginUser(String username) {
        if (null == getAllUser()) {
            return null;
        }
        return Objects.requireNonNull(getAllUser()).parallelStream().filter(a -> a.getUsername().equals(username)).findAny().orElse(null);
    }

    public static boolean createUser(String username, String password){
        User inputUser = new User();
        inputUser.setUsername(username);
        inputUser.setPassword(password);
        inputUser.setTablePermissions(new ArrayList<>());
        inputUser.setDataBasePermissions(new ArrayList<>());
        List<User> allUser = getAllUser();
        if(allUser == null) allUser = new ArrayList<>();
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream oOutputStream = null;
        try {
            if (null == allUser)
                new File(userDbFile).createNewFile();
            fileOutputStream = new FileOutputStream(userDbFile);
            oOutputStream = new ObjectOutputStream(fileOutputStream);
            if (allUser.stream().filter(a->a.getUsername().equals(inputUser.getUsername())).count() > 0) {
                System.out.println("ERROR: 用户已存在，无法创建");
            }
            allUser.add(inputUser);
            oOutputStream.writeObject(allUser);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean updateUser(User inputUser){
        List<User> allUser = getAllUser();
        if(allUser == null) allUser = new ArrayList<>();
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream oOutputStream = null;
        try {
            if (null == allUser)
                new File(userDbFile).createNewFile();
            fileOutputStream = new FileOutputStream(userDbFile);
            oOutputStream = new ObjectOutputStream(fileOutputStream);
            allUser.forEach(u->{
                if (u.getUsername().equals(inputUser.getUsername())) {
                    u.setTablePermissions(inputUser.getTablePermissions());
                    u.setDataBasePermissions(inputUser.getDataBasePermissions());
                }
            });
            oOutputStream.writeObject(allUser);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oOutputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    private static List<User> getAllUser(){
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        if (!new File(userDbFile).exists()) return null;
        try {
            fileInputStream = new FileInputStream(userDbFile);
            objectInputStream = new ObjectInputStream(fileInputStream);
            List<User> list = (List<User>) objectInputStream.readObject();
            return list;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new ArrayList<>();
    }
}
